﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Data;
using System.Data.Common;
using System.Data.OleDb;

namespace MicroDal.Providers
{
    /// <summary>
    /// This implementation can be used for testing purpose
    /// </summary>
    internal class TestingResource : IDbResource
    {
        public TestingResource(string connectionstring)
        {
        }
        public object ExecuteScalar(string sql, System.Data.CommandType commandType, IEnumerable<DbParameter> parameters)
        {
            WriteCommandParameters(sql, commandType, parameters);
            return null;
        }

        public int Execute(string sql, System.Data.CommandType commandType, IEnumerable<DbParameter> parameters)
        {
            WriteCommandParameters(sql, commandType, parameters);
            return -1;
        }

        public IEnumerable<T> Fetch<T>(string sql, System.Data.CommandType commandType, IEnumerable<DbParameter> parameters, Func<System.Data.Common.DbDataReader, T> mapper)
        {
            WriteCommandParameters(sql, commandType, parameters);
            yield break;
        }

        public T FetchOne<T>(string sql, System.Data.CommandType commandType, IEnumerable<DbParameter> parameters, Func<System.Data.Common.DbDataReader, T> mapper)
        {
            WriteCommandParameters(sql, commandType, parameters);
            return default(T);
        }

        private void WriteCommandParameters(string sql, System.Data.CommandType commandType, IEnumerable<DbParameter> parameters)
        {
            Debug.WriteLine(string.Format("Query: {0}", sql));
            Debug.WriteLine(string.Format("Type: {0}", commandType));
            Debug.WriteLine("Parameters: ");

            foreach (DbParameter param in parameters)
            {
                Debug.WriteLine(string.Format("\t{0}: [{1}]", param.ParameterName, param.Value));
            }
        }

        public T GetLastInsertedId<T>()
        {
            return default(T);
        }

        public void Dispose()
        {
        }


        public Func<ParameterConfig, object, DbParameter> ParameterInitializer
        {
            get
            {
                return (c, v) => new OleDbParameter(c.Name, v);
            }
        }
        public string ParameterPattern { get { return @"[^@]?(?<name>@\w+)"; } }


        public DbTransaction GetTransaction(IsolationLevel isolationLevel)
        {
            return new Transaction();
        }

        public class Transaction : DbTransaction
        {
            public override void Commit()
            {
//                throw new NotImplementedException();
            }

            protected override DbConnection DbConnection
            {
                get { throw new NotImplementedException(); }
            }

            public override IsolationLevel IsolationLevel
            {
                get { throw new NotImplementedException(); }
            }

            public override void Rollback()
            {
  //              throw new NotImplementedException();
            }
        }
    }
}
